<?php

namespace app\index\controller;

use think\captcha\Captcha;
use think\Controller;
use think\Loader;
use think\Request;
use app\index\model\Userlogin as UserLoginModel;
class Userlogin extends Controller
{
    /**
     * 前台用户的登录
     *
     * @return \think\Response
     */
    public function login()
    {
        if (\request()->isPost()){
            //将登录过来的数据传给模型，让模型去处理
            $res=(new UserLoginModel())->login(input('post.'));
            if ($res['valid'])
            {
//                $this->redirect('index/userlogin/login',[], 5, '页面跳转中...')->restore();
                $this->success('登录成功','index/index/index');
            }else{
                //执行登录失败
                $this->error($res['msg']);
            }
        }

        return view();
    }


    //前台的用户注册
    public function register(){
        //获取post数据
        $post=input('post.');
        if (\request()->isPost()){
            $validate = Loader::validate('Userlogin');
            if(!$validate->check($post)){
                $this->error($validate->getError());
            }
            //验证验证码
            $captcha = new Captcha();
            $captcha->check($post['code']);


            $post['utime']=time();
            $user=new UserLoginModel();

            $post['password']=md5(md5($post['password']));
            $res=$user->allowField( true )->save( $post );
            if ($res){
                $this->success('注册成功,请登录','index/userlogin/login');
            }
        }

        return view();
    }
}
